package com.atguigu.yygh.user.controller;

import com.atgui.yygh.model.user.UserInfo;
import com.atgui.yygh.vo.user.LoginVo;
import com.atgui.yygh.vo.user.UserAuthVo;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.utils.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * zz: wzh
 * time 2022 09 06 22 00
 **/
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;
    //用户登录
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo){
        Map<String,Object> map=userInfoService.loginUser(loginVo);
        return Result.ok(map);
    }
    //用户认证
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //传递两个参数,第一个是id,第二个是认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }
    //获取用户认证信息的接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
